package com.collect.zambia.zhiScene.zhiCollection.zhiDetail;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.collect.zambia.R;
import com.collect.zambia.zhiAdater.zhiD01Records.ZHID01RecordAdjuster;
import com.collect.zambia.zhiAdater.zhiHistoryOrders.ZHIHistoryOrderAdjuster;
import com.collect.zambia.zhiAdater.zhiPhoneReview.ZHIPhoneAdjusterClickListener;
import com.collect.zambia.zhiAdater.zhiPhoneReview.ZHIPhoneSelfAdjuster;
import com.collect.zambia.zhiAdater.zhiRecTagAdjuster.ZHIRecTag;
import com.collect.zambia.zhiAdater.zhiRecTagAdjuster.ZHIRecTagAdjuster;
import com.collect.zambia.zhiBase.BaseActivity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRBankCardEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCaseDetailD01FollowEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCaseDetailEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCaseDetailHistoryOrderEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIROrderEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRPartRepayListEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRPhoneBookEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRPhoneBookSmsEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRRecTagEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRRepaymentLinkEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRWarnLetterEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISCaseDetailEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISCaseRecordEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISCreateDunVirtualAccountEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISPartRepayListEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISRepaymentLinkEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISSendSmsEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISSendWarnLetterEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISSmsContextEntity;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectConstants;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.zambia.zhiGlobal.zhiHandler.ZHIProgressDialogHandler;
import com.collect.zambia.zhiHelper.ZHIMainProcessHelper;
import com.collect.zambia.zhiHelper.ZHIPhoneCallHelper;
import com.collect.zambia.zhiHelper.ZHISysShareHelper;
import com.collect.zambia.zhiScene.zhiWidget.zhiBar.ZHICircleProgressbar;
import com.collect.zambia.zhiScene.zhiWidget.zhiDialog.ZHIDialogAvatar;
import com.collect.zambia.zhiScene.zhiWidget.zhiDialog.ZHIDialogMessage;
import com.collect.zambia.zhiScene.zhiWidget.zhiDialog.ZHIDialogChangeAmount;
import com.collect.zambia.zhiScene.zhiWidget.zhiDialog.ZHIDialogPartRepay;
import com.collect.zambia.zhiScene.zhiWidget.zhiNavigationBar.ZHINavigationBar;
import com.collect.zambia.zhiScene.zhiWidget.zhiPhonebookView.ZHIPhoneBookView;
import com.collect.zambia.zhiScene.zhiWidget.zhiRecyclerView.ZHIRecyclerView;
import com.collect.zambia.zhiScene.zhiWidget.zhiSendSmsView.ZHISendRepaymentLinkView;
import com.collect.zambia.zhiScene.zhiWidget.zhiSendSmsView.ZHISendSmsClickListener;
import com.collect.zambia.zhiScene.zhiWidget.zhiSendSmsView.ZHISendSmsLinkView;
import com.collect.zambia.zhiScene.zhiWidget.zhiSendSmsView.ZHISendSmsView;
import com.collect.zambia.zhiScene.zhiWidget.zhiSendSmsView.ZHISendWarnLetterView;
import com.collect.zambia.zhiScene.zhiWidget.zhiStepView.ZHINextStepView;
import com.collect.zambia.zhiScene.zhiWidget.zhiSubmitRecTagView.ZHISubmitRecTagView;
import com.collect.zambia.zhiUtils.StringUtil;
import com.collect.zambia.zhiUtils.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_Detail)
public class ZHIDetailActivity extends BaseActivity<ZHIDetailPresenter> implements ZHIDetailContract.View {

    private static final String STATE_USER_ID = "userId";
    private static final String STATE_ORDER_ID = "orderId";
    private static final String STATE_APPID = "appId";

    @BindView(R.id.navigationBar)
    ZHINavigationBar navigationBar;

    @BindView(R.id.btn_self_rec_tag)
    ZHINextStepView btn_self_rec_tag;

    //user
    @BindView(R.id.tv_cu_name)
    TextView tv_cu_name;
    @BindView(R.id.iv_cu_avator)
    ImageView iv_cu_avator;
    @BindView(R.id.rc_cu_mobile_list)
    ZHIRecyclerView rc_cu_mobile_list;


    //loan amount
    @BindView(R.id.tv_la_amount)
    TextView tv_la_amount;
    @BindView(R.id.tv_la_overdue_days)
    TextView tv_la_overdue_days;
    @BindView(R.id.ll_la_amount_content)
    LinearLayout ll_la_amount_content;
    @BindView(R.id.tv_la_principal)
    TextView tv_la_principal;
    @BindView(R.id.tv_la_interest)
    TextView tv_la_interest;
    @BindView(R.id.tv_la_overdue_fee)
    TextView tv_la_overdue_fee;
    @BindView(R.id.tv_la_lending_time)
    TextView tv_la_lending_time;
    @BindView(R.id.tv_la_repay_time)
    TextView tv_la_repay_time;
    @BindView(R.id.tv_la_repay_amount)
    TextView tv_la_repay_amount;
    @BindView(R.id.tv_la_target_app_name)
    TextView tv_la_target_app_name;
    @BindView(R.id.tv_la_target_app_link)
    TextView tv_la_target_app_link;
    @BindView(R.id.tv_la_repay_process_fee)
    TextView tv_la_repay_process_fee;
    @BindView(R.id.tv_la_amount_already_repaid)
    TextView tv_la_amount_already_repaid;
    @BindView(R.id.iv_la_amount_detail_ext)
    ImageView iv_la_amount_detail_ext;
    @BindView(R.id.iv_cu_settle_logo)
    ImageView iv_cu_settle_logo;
    @BindView(R.id.tv_la_promised_due_time)
    TextView tv_la_promised_due_time;

    //repya link
    @BindView(R.id.tv_la_repayment_link)
    TextView tv_la_repayment_link;

    //record tag
    @BindView(R.id.tv_tr_record_tag_count)
    TextView tv_tr_record_tag_count;
    @BindView(R.id.rc_tr_record_tag_list)
    ZHIRecyclerView rc_tr_record_tag_list;
    @BindView(R.id.iv_tr_record_tag_ext)
    ImageView iv_tr_record_tag_ext;
    @BindView(R.id.ll_tr_rec_tag_content)
    LinearLayout ll_tr_rec_tag_content;

    //record do1
    @BindView(R.id.tv_dr_d01_record_count)
    TextView tv_dr_d01_record_count;
    @BindView(R.id.rc_dr_d01_record_list)
    ZHIRecyclerView rc_dr_d01_record_list;
    @BindView(R.id.iv_dr_d01_record_ext)
    ImageView iv_dr_d01_record_ext;
    @BindView(R.id.ll_dr_d01_record_content)
    LinearLayout ll_dr_d01_record_content;


    //history order
    @BindView(R.id.iv_ho_history_order_ext)
    ImageView iv_ho_history_order_ext;
    @BindView(R.id.rc_ho_history_order_list)
    ZHIRecyclerView rc_ho_history_order_list;
    @BindView(R.id.ll_ho_history_order_content)
    LinearLayout ll_ho_history_order_content;


    //full name
    @BindView(R.id.tv_fn_full_name)
    TextView tv_fn_full_name;
    @BindView(R.id.tv_fn_target_gender)
    TextView tv_fn_target_gender;
    @BindView(R.id.tv_fn_target_marital_status)
    TextView tv_fn_target_marital_status;
    @BindView(R.id.tv_fn_target_dob)
    TextView tv_fn_target_dob;
    @BindView(R.id.tv_fn_target_address)
    TextView tv_fn_target_address;
    @BindView(R.id.tv_fn_target_type)
    TextView tv_fn_target_type;
    @BindView(R.id.iv_fn_full_name_ext)
    ImageView iv_fn_full_name_ext;
    @BindView(R.id.ll_fn_full_name_content)
    LinearLayout ll_fn_full_name_content;
    @BindView(R.id.tv_fn_target_bvn)
    TextView tv_fn_target_bvn;
    @BindView(R.id.tv_fn_target_company_name)
    TextView tv_fn_target_company_name;
    @BindView(R.id.tv_fn_target_company_address)
    TextView tv_fn_target_company_address;
    @BindView(R.id.tv_fn_target_job_type)
    TextView tv_fn_target_job_type;
    @BindView(R.id.tv_fn_target_company_city)
    TextView tv_fn_target_company_city;
    @BindView(R.id.tv_fn_target_start_working_there)
    TextView tv_fn_target_start_working_there;
    @BindView(R.id.tv_fn_target_monthly_income)
    TextView tv_fn_target_monthly_income;
    @BindView(R.id.tv_fn_target_residence)
    TextView tv_fn_target_residence;


    //bankcard
    @BindView(R.id.tv_bc_bank_account)
    TextView tv_bc_bank_account;
    @BindView(R.id.tv_bc_target_holder_name)
    TextView tv_bc_target_holder_name;
    @BindView(R.id.tv_bc_target_isfc_code)
    TextView tv_bc_target_isfc_code;
    @BindView(R.id.tv_bc_target_bankcard_no)
    TextView tv_bc_target_bankcard_no;
    @BindView(R.id.iv_bc_bankcard_ext)
    ImageView iv_bc_bankcard_ext;
    @BindView(R.id.ll_bc_bankcard_content)
    LinearLayout ll_bc_bankcard_content;


    //relationship
    @BindView(R.id.tv_rs_target_relationship_name0)
    TextView tv_rs_target_relationship_name0;
    @BindView(R.id.tv_rs_target_relationship_phone0)
    TextView tv_rs_target_relationship_phone0;
    @BindView(R.id.tv_rs_target_relationship_relationship0)
    TextView tv_rs_target_relationship_relationship0;
    @BindView(R.id.tv_rs_target_relationship_name1)
    TextView tv_rs_target_relationship_name1;
    @BindView(R.id.tv_rs_target_relationship_phone1)
    TextView tv_rs_target_relationship_phone1;
    @BindView(R.id.tv_rs_target_relationship_relationship1)
    TextView tv_rs_target_relationship_relationship1;
    @BindView(R.id.iv_rs_relation_ship_ext)
    ImageView iv_rs_relation_ship_ext;
    @BindView(R.id.ll_rs_relationship_content0)
    LinearLayout ll_rs_relationship_content0;
    @BindView(R.id.ll_rs_relationship_content1)
    LinearLayout ll_rs_relationship_content1;
    @BindView(R.id.ll_rs_relationship_content)
    LinearLayout ll_rs_relationship_content;


    //offline repay
    @BindView(R.id.iv_or_offline_repay_ext)
    ImageView iv_or_offline_repay_ext;
    @BindView(R.id.tv_or_target_offline_amount)
    TextView tv_or_target_offline_amount;
    @BindView(R.id.tv_or_target_recipient_account_name)
    TextView tv_or_target_recipient_account_name;
    @BindView(R.id.tv_or_target_recipient_bank_account_no)
    TextView tv_or_target_recipient_bank_account_no;
    @BindView(R.id.tv_or_target_recipient_bank_account_name)
    TextView tv_or_target_recipient_bank_account_name;
    @BindView(R.id.ll_or_target_offline_repay_content)
    LinearLayout ll_or_target_offline_repay_content;
    @BindView(R.id.cp_or_progress)
    ZHICircleProgressbar cp_or_progress;
    @BindView(R.id.tv_or_target_offline_account_get)
    TextView tv_or_target_offline_account_get;
    @BindView(R.id.tv_or_countdown)
    TextView tv_or_countdown;
    @BindView(R.id.rl_or_cutdown_bg)
    RelativeLayout rl_or_cutdown_bg;


    @Autowired
    public String orderId;
    @Autowired
    public String userId;
    @Autowired
    public String appId;

    private String mSelfRelation = "SELF";
    private String mOriAmount = "";
    private String mRepaymentAmount = "";
    private String mProductName = "";
    private String mEmail = "";
    private String mLeven = "";
    private String mMobile = "";
    private String mAvator = "";

    private ZHISubmitRecTagView mZHISubmitRecTagView;
    private ZHIPhoneBookView mZHIPhoneBookView;
    private ZHIProgressDialogHandler mZHIProgressDialogHandler;
    private ZHISendSmsView mZHISendSmsView;
    private ZHISendRepaymentLinkView mZHISendRepaymentLinkView;
    private ZHISendSmsLinkView mZHISendSmsLinkView;
    private ZHIDialogChangeAmount mZHIDialogChangeAmount;
    private ZHISendWarnLetterView mZHISendWarnLetterView;

    private ArrayList<ZHIRPhoneBookEntity> mPhoneBookEntities;

    private final ZHIRecTagAdjuster mZHIRecTagAdjuster = new ZHIRecTagAdjuster(new ArrayList<>());
    private ArrayList<ZHIRecTag> mRecTagArrayList;

    private final ZHID01RecordAdjuster mZHID01RecordAdjuster = new ZHID01RecordAdjuster();

    private final ZHIHistoryOrderAdjuster mZHIHistoryOrderAdjuster = new ZHIHistoryOrderAdjuster();

    private final ZHIPhoneSelfAdjuster mZHIPhoneSelfAdjuster = new ZHIPhoneSelfAdjuster();

    private ZHIDialogMessage dialogMessage;

    private boolean mIsFetchVirtualData = false;
    private boolean mIsSettled = false;
    private boolean mIsFetchSmsBothContacts = false;
    private boolean mIsFetchPhonebooks = false;

    private static final int MAX_CUTDOWN_SEC = 10;

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_detail_v2;
    }

    @Override
    protected void checkSavedInstanceState(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        // 检查是否正在重新创建一个以前销毁的实例
        if (savedInstanceState != null) {
            // 从已保存状态恢复成员的值
            appId = savedInstanceState.getString(STATE_APPID);
            orderId = savedInstanceState.getString(STATE_ORDER_ID);
            userId = savedInstanceState.getString(STATE_USER_ID);
        }
    }

    @Override
    protected void initView() {
        try {
            navigationBar.hideRight().setCenterText(getResources().getString(R.string.case_info)).setBarClickListener(new ZHINavigationBar.OnNavigationBarClickListener() {
                @Override
                public void onNavigationBarLeftClick(View v) {
                    finish();
                }

                @Override
                public void onNavigationBarRightClick(View v) {
                    ZHIProjectRouter.toWordsSkills();
                }
            });
            navigationBar.setLeftImgage(R.drawable.ic_91);
            navigationBar.setRightImgage(R.drawable.ic_132);
            navigationBar.showLeft();
            navigationBar.showRight();

            mZHIDialogChangeAmount = new ZHIDialogChangeAmount(getActivity());
            mZHIPhoneBookView = new ZHIPhoneBookView(getActivity());
            mZHISubmitRecTagView = new ZHISubmitRecTagView(getActivity());
            mZHISendSmsView = new ZHISendSmsView(getActivity());
            mZHISendRepaymentLinkView = new ZHISendRepaymentLinkView(getActivity());
            mZHISendSmsLinkView = new ZHISendSmsLinkView(getActivity());
            mZHISendWarnLetterView = new ZHISendWarnLetterView(getActivity());

            initEvent();
            initData();
            initTempHUD();
            initCutdown();

            //初始化电话模块
            ZHIPhoneCallHelper.getInstance().initPhoneCall(getActivity(), ZHIPhoneCallHelper.ZHIPhoneCallType.CS);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // 保存用户自定义的状态
        savedInstanceState.putString(STATE_APPID, appId);
        savedInstanceState.putString(STATE_ORDER_ID, orderId);
        savedInstanceState.putString(STATE_USER_ID, userId);

        // 调用父类交给系统处理，这样系统能保存视图层次结构状态
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mZHIProgressDialogHandler != null) {
            mZHIProgressDialogHandler.hide();
        }
    }

    @Override
    protected void onDestroy() {
        try {
            //移除电话模块
            ZHIPhoneCallHelper.getInstance().stopPhoneCall(getActivity());

            stopCutdown();
            if (mHanler != null) {
                if (mFinishRunable != null) {
                    mHanler.removeCallbacks(mFinishRunable);
                }
                mHanler = null;
            }
            if (mFinishRunable != null) {
                mFinishRunable = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    /**
     * 设置监听事件
     *
     * @type method
     * @author liwenjie
     * @creatTime 2019-11-04 11:03
     */
    public void initEvent() {
        try {
            iv_cu_avator.setOnClickListener(v -> {
                try {
                    if (!TextUtils.isEmpty(mAvator) && mAvator.startsWith("http")) {
                        ZHIDialogAvatar.showDialog(ZHIDetailActivity.this, mAvator);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            btn_self_rec_tag.setOnMenuItemDidChangedListener(item -> showRecTagSubmitView(tv_fn_full_name.getText().toString().trim(), mMobile, mSelfRelation));
            mZHIPhoneBookView.setPhoneBookClickListener(new ZHIPhoneBookView.OnPhoneBookClickListener() {
                @Override
                public void onRecTagClick(ZHIRPhoneBookEntity phoneBookEntity) {
                    if (mIsSettled) {
                        return;
                    }
                    String relation = phoneBookEntity.getRelation();
                    if (TextUtils.isEmpty(relation)) {
                        if (phoneBookEntity.isOftenUsed()) {
                            relation = "Often used";
                        }
                    }
                    showRecTagSubmitView(phoneBookEntity.getContactName(), phoneBookEntity.getContactMobile(), relation);
                }

                @Override
                public void onCallPhoneClick(ZHIRPhoneBookEntity phoneBookEntity) {
                    if (mIsSettled) {
                        return;
                    }

                    //统一控制电话操作
                    ZHIPhoneCallHelper.getInstance().startPhoneCall(getActivity(), phoneBookEntity.getContactMobile(), orderId);
                    //ZHISysShareHelper.callPhone(MyApplication.getAppContext(), phoneBookEntity.getContactMobile());
                }

                @Override
                public void onWhatsappClick(ZHIRPhoneBookEntity phoneBookEntity) {
                    if (mIsSettled) {
                        return;
                    }
//                    ZHISysShareHelper.sendMsgWithWhatsApp(ZHIDetailActivity.this,  ZHIProjectConstants.PRE_AREA_FLAG + phoneBookEntity.getContactMobile(), "");

                    String relation = phoneBookEntity.getRelation();
                    if (TextUtils.isEmpty(relation)) {
                        if (phoneBookEntity.isOftenUsed()) {
                            relation = "Often used";
                        }
                    }

                    ZHISCaseRecordEntity zhiSCaseRecordEntity = getCaseRecordParamsForWhatsapp(phoneBookEntity.getContactName(), phoneBookEntity.getContactMobile(), relation);
                    assert mPresenter != null;
                    mPresenter.caseRecordForWhatsapp(ZHIDetailActivity.this, zhiSCaseRecordEntity, "");
                }
            });
            mZHISubmitRecTagView.setSubmitRecTagItemClickListener(() -> {
                if (mIsSettled) {
                    return;
                }
                ZHISCaseRecordEntity zhiSCaseRecordEntity = getCaseRecordParams();
                assert mPresenter != null;
                mPresenter.caseRecord(ZHIDetailActivity.this, zhiSCaseRecordEntity);
            });
            mZHISendSmsView.setOnSendSmsClickListener(new ZHISendSmsClickListener() {
                @Override
                public void onSendSmsClick(String content) {
                    if (mIsSettled) {
                        return;
                    }
                    ZHISSendSmsEntity zhiSSendSmsEntity = getSendSmsParamsV2(content);
                    assert mPresenter != null;
                    mPresenter.sendTagSmsV2(getActivity(), zhiSSendSmsEntity);
                }

                @Override
                public void onSendSmsWithWhatsAppClick(String mobile, String content) {
                    if (mIsSettled) {
                        return;
                    }
//                    ZHISysShareHelper.sendMsgWithWhatsApp(ZHIDetailActivity.this,  ZHIProjectConstants.PRE_AREA_FLAG + mobile, content);

                    String relation = "";
                    ZHISCaseRecordEntity zhiSCaseRecordEntity = getCaseRecordParamsForWhatsapp(mZHISendSmsView.getName(), mobile, relation);
                    assert mPresenter != null;
                    mPresenter.caseRecordForWhatsapp(ZHIDetailActivity.this, zhiSCaseRecordEntity, content);
                }
            });
            mZHISendRepaymentLinkView.setOnSendRepaymentLinkClickListener(new ZHISendSmsClickListener() {
                @Override
                public void onSendSmsClick(String content) {
                    if (mIsSettled) {
                        return;
                    }
                    ZHISSendSmsEntity zhisSendSmsEntity = getSendSmsLinkParams(content);
                    assert mPresenter != null;
                    mPresenter.sendTagSmsLink(getActivity(), zhisSendSmsEntity);
                }

                @Override
                public void onSendSmsWithWhatsAppClick(String mobile, String content) {
                    if (mIsSettled) {
                        return;
                    }
//                    ZHISysShareHelper.sendMsgWithWhatsApp(ZHIDetailActivity.this,  ZHIProjectConstants.PRE_AREA_FLAG + mobile, content);

                    String relation = "";
                    ZHISCaseRecordEntity zhiSCaseRecordEntity = getCaseRecordParamsForWhatsapp(mZHISendRepaymentLinkView.getName(), mobile, relation);
                    assert mPresenter != null;
                    mPresenter.caseRecordForWhatsapp(ZHIDetailActivity.this, zhiSCaseRecordEntity, content);
                }
            });

            mZHISendWarnLetterView.setOnSendSmsClickListener(new ZHISendSmsClickListener() {
                @Override
                public void onSendSmsClick(String content) {

                }

                @Override
                public void onSendSmsWithWhatsAppClick(String mobile, String content) {
                    ZHIDialogMessage.showDialog(ZHIDetailActivity.this, "Prompt", "Has it been successfully sent to the customer?", "NO", "YES", tag -> {
                        if (mPresenter != null) {
                            mPresenter.getUserDemandLetterPdfUrl(ZHIDetailActivity.this, new ZHISSendWarnLetterEntity(orderId, "YES"));
                        }
                    });
                    new Handler(Looper.getMainLooper()).postDelayed(() -> ZHISysShareHelper.sendMsgWithWhatsApp(ZHIDetailActivity.this, ("260" + mobile), content), 800);
                }
            });

            mZHISendSmsLinkView.setOnSendSmsLinkClickListener(new ZHISendSmsClickListener() {
                @Override
                public void onSendSmsClick(String content) {
                    if (mIsSettled) {
                        return;
                    }
                    ZHISSendSmsEntity zhiSSendSmsEntity = getSendSmsParamsV2(content);
                    Log.d(TAG, "onSendSmsClick: " + content);
                    assert mPresenter != null;
                    mPresenter.sendTagSmsV2(getActivity(), zhiSSendSmsEntity);
                }

                @Override
                public void onSendSmsWithWhatsAppClick(String mobile, String content) {
                    if (mIsSettled) {
                        return;
                    }
//                    ZHISysShareHelper.sendMsgWithWhatsApp(ZHIDetailActivity.this,  ZHIProjectConstants.PRE_AREA_FLAG + mobile, content);

                    String relation = "";
                    ZHISCaseRecordEntity zhiSCaseRecordEntity = getCaseRecordParamsForWhatsapp(mZHISendSmsLinkView.getName(), mobile, relation);
                    assert mPresenter != null;
                    mPresenter.caseRecordForWhatsapp(ZHIDetailActivity.this, zhiSCaseRecordEntity, content);
                }
            });

            mZHIPhoneSelfAdjuster.setPhoneAdjusterClickListener((type, extraData) -> {
                if (type == ZHIPhoneAdjusterClickListener.PHONE_CLICK_TYPE_SMS) {
                    if (mIsSettled) {
                        return;
                    }
                    //发短信
                    //请求短信内容
                    ZHISSmsContextEntity zhisSmsContextEntity = new ZHISSmsContextEntity(orderId, "CS", mRepaymentAmount, tv_cu_name.getText().toString().trim());
                    assert mPresenter != null;
                    mPresenter.getSmsContext(ZHIDetailActivity.this, zhisSmsContextEntity);
                    //showSendSmsView(tv_fn_full_name.getText().toString().trim(), extraData);
                }
            });

            rc_cu_mobile_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_cu_mobile_list.setAdapter(mZHIPhoneSelfAdjuster);

            rc_tr_record_tag_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_tr_record_tag_list.setAdapter(mZHIRecTagAdjuster);

            rc_dr_d01_record_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_dr_d01_record_list.setAdapter(mZHID01RecordAdjuster);

            rc_ho_history_order_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_ho_history_order_list.setAdapter(mZHIHistoryOrderAdjuster);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置数据源
     *
     * @type method
     * @author liwenjie
     * @creatTime 2019-11-05 09:52
     */
    public void initData() {
        assert mPresenter != null;
        mPresenter.getCaseDetailV2(ZHIDetailActivity.this, new ZHISCaseDetailEntity(userId, orderId, appId));
    }

    @Override
    public void onGetErrorInfo() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetCaseDetailV2(ZHIRCaseDetailEntity zhirCaseDetailEntity) {
        try {
            ZHIROrderEntity orderEntity = zhirCaseDetailEntity.getOrderInfo();

            mProductName = StringUtil.safeString(orderEntity.getAppName());
            mEmail = StringUtil.safeString(orderEntity.getEmail());
            mLeven = StringUtil.safeString(orderEntity.getLeven());
            mRepaymentAmount = StringUtil.safeString(orderEntity.getRepayAmount());
            mOriAmount = StringUtil.safeString(orderEntity.getPrincipal());
            mMobile = StringUtil.safeString(orderEntity.getMobile());
            mAvator = StringUtil.safeString(orderEntity.getPhotoUrl());
            mIsSettled = orderEntity.isSettleStatus();

            //user
            tv_cu_name.setText(orderEntity.getFullName());
            iv_cu_avator.setVisibility(TextUtils.isEmpty(orderEntity.getPhotoUrl()) ? View.GONE : View.VISIBLE);
            iv_cu_settle_logo.setVisibility(mIsSettled ? View.VISIBLE : View.GONE);

            if (View.VISIBLE == iv_cu_avator.getVisibility() && !TextUtils.isEmpty(mAvator)) {
                Glide.with(ZHIDetailActivity.this).load(mAvator).into(iv_cu_avator);
            }

            //settle
            btn_self_rec_tag.setVisibility(mIsSettled ? View.GONE : View.VISIBLE);

            //如果是查询创建虚拟账号的数据
            if (mIsFetchVirtualData) {
                mIsFetchVirtualData = false;

                tv_or_target_offline_account_get.setVisibility(View.VISIBLE);
                tv_or_target_offline_amount.setText(ZHIProjectConstants.RS_FLAG + StringUtil.numberTothousandFormatter(orderEntity.getRepayAmount()));
                tv_or_target_recipient_account_name.setText(orderEntity.getOfflineRepayBankName());
                tv_or_target_recipient_bank_account_no.setText(orderEntity.getOfflineRepayBankAccount());
                tv_or_target_recipient_bank_account_name.setText(orderEntity.getOfflineRepayBankAccountName());
                return;
            }

            //loan amount
            tv_la_overdue_days.setText("Days Overdue: " + orderEntity.getOverdueDay());
            tv_la_amount.setText(ZHIProjectConstants.RS_FLAG + StringUtil.numberTothousandFormatter(orderEntity.getRepayAmount()));
            tv_la_principal.setText(ZHIProjectConstants.RS_FLAG + StringUtil.numberTothousandFormatter(orderEntity.getPrincipal()));
            tv_la_interest.setText(ZHIProjectConstants.RS_FLAG + StringUtil.numberTothousandFormatter(orderEntity.getInterest()));
            tv_la_overdue_fee.setText(ZHIProjectConstants.RS_FLAG + StringUtil.numberTothousandFormatter(orderEntity.getOverdueFee()));
            tv_la_repay_process_fee.setText(ZHIProjectConstants.RS_FLAG + StringUtil.numberTothousandFormatter(orderEntity.getRepayProcessFee()));
            tv_la_lending_time.setText(orderEntity.getLoanTime());
            tv_la_repay_time.setText(orderEntity.getRepayTime());
            tv_la_repay_amount.setText(ZHIProjectConstants.RS_FLAG + StringUtil.numberTothousandFormatter(orderEntity.getRepayAmount()));
            tv_la_target_app_name.setText(orderEntity.getAppName());
            tv_la_target_app_link.setText(orderEntity.getAppLink());
            tv_la_amount_already_repaid.setText(ZHIProjectConstants.RS_FLAG + StringUtil.numberTothousandFormatter(orderEntity.getPartRepayAmount()));
            tv_la_promised_due_time.setText(orderEntity.getPtpTime());

            //full name
            tv_fn_full_name.setText(orderEntity.getFullName());
            tv_fn_target_gender.setText(orderEntity.getGender());
            tv_fn_target_marital_status.setText(orderEntity.getMaritalStatus());
            tv_fn_target_dob.setText(orderEntity.getBirthDate());
            tv_fn_target_address.setText(orderEntity.getProofAddressDetail());
            tv_fn_target_type.setText(orderEntity.getUserType());
            tv_fn_target_bvn.setText(orderEntity.getBvnNumber());
            tv_fn_target_company_name.setText(orderEntity.getCompanyName());
            tv_fn_target_company_address.setText(orderEntity.getCompanyAddress());
            tv_fn_target_company_city.setText(orderEntity.getCompanyCity());
            tv_fn_target_job_type.setText(orderEntity.getEmploymentType());
            tv_fn_target_start_working_there.setText(orderEntity.getWorkingSince());
            tv_fn_target_monthly_income.setText(orderEntity.getMonthlyIncome());
            tv_fn_target_residence.setText(orderEntity.getCurrResidence());

            //repay link
            if (orderEntity.isRepayLinkTimesMaxLimit()) {
                tv_la_repayment_link.setBackgroundResource(R.drawable.bg_stroke_theme_black_5_width_1dp_corners_4dp);
                tv_la_repayment_link.setTextColor(ContextCompat.getColor(ZHIDetailActivity.this, R.color.theme_black_5));
            } else {
                tv_la_repayment_link.setBackgroundResource(R.drawable.bg_stroke_theme_color_0_width_1dp_corners_4dp);
                tv_la_repayment_link.setTextColor(ContextCompat.getColor(ZHIDetailActivity.this, R.color.theme_color_0));
            }

//            tv_or_target_offline_account_get.setVisibility(orderEntity.isHadOfflineAccount() ? View.GONE : View.VISIBLE);
            tv_or_target_offline_account_get.setVisibility(View.VISIBLE);
            tv_or_target_offline_amount.setText(ZHIProjectConstants.RS_FLAG + StringUtil.numberTothousandFormatter(orderEntity.getRepayAmount()));
            tv_or_target_recipient_account_name.setText(orderEntity.getOfflineRepayBankName());
            tv_or_target_recipient_bank_account_no.setText(orderEntity.getOfflineRepayBankAccount());
            tv_or_target_recipient_bank_account_name.setText(orderEntity.getOfflineRepayBankAccountName());

            //bank card
            ZHIRBankCardEntity bankCardEntity = zhirCaseDetailEntity.getBankCard();
            tv_bc_bank_account.setText(bankCardEntity.getCardNo());
            tv_bc_target_isfc_code.setText(bankCardEntity.getBankCode());
            tv_bc_target_holder_name.setText(bankCardEntity.getCardName());
            tv_bc_target_bankcard_no.setText(bankCardEntity.getCardNo());

            ///////////////////////////////user mobile list
            ArrayList<String> mobileList = orderEntity.getMobileList();
            //mobileList.add(0, orderEntity.getMobile());
            mZHIPhoneSelfAdjuster.setNewData(mobileList);

            //////////////////////////////phone book list
            ArrayList<ZHIRPhoneBookEntity> phoneBookEntities = zhirCaseDetailEntity.getDunFollows();
            ZHIRPhoneBookEntity relationEntity0 = null;
            ZHIRPhoneBookEntity relationEntity1 = null;
            ZHIRPhoneBookEntity relationEntity2 = null;
            if (phoneBookEntities.size() >= 3) {
                relationEntity0 = phoneBookEntities.get(0);
                relationEntity1 = phoneBookEntities.get(1);
                relationEntity2 = phoneBookEntities.get(2);
            }
            if (relationEntity0 != null) {
                mSelfRelation = relationEntity0.getRelation();
            }
            if (relationEntity1 != null) {
                ll_rs_relationship_content0.setVisibility(View.VISIBLE);
                tv_rs_target_relationship_name0.setText(relationEntity1.getContactName());
                tv_rs_target_relationship_phone0.setText(relationEntity1.getContactMobileEncryptFormatter());
                tv_rs_target_relationship_phone0.setTag(relationEntity1.getContactMobile());
                tv_rs_target_relationship_relationship0.setText(relationEntity1.getRelation());
            } else {
                ll_rs_relationship_content0.setVisibility(View.GONE);
            }
            if (relationEntity2 != null) {
                ll_rs_relationship_content1.setVisibility(View.VISIBLE);
                tv_rs_target_relationship_name1.setText(relationEntity2.getContactName());
                tv_rs_target_relationship_phone1.setText(relationEntity2.getContactMobileEncryptFormatter());
                tv_rs_target_relationship_phone1.setTag(relationEntity2.getContactMobile());
                tv_rs_target_relationship_relationship1.setText(relationEntity2.getRelation());
            } else {
                ll_rs_relationship_content1.setVisibility(View.GONE);
            }
            mPhoneBookEntities = zhirCaseDetailEntity.getDunFollows();

            //////////////////////////////
//            ArrayList<ZHIRRecTagEntity> mRecTagEntities = zhirCaseDetailEntity.getDunRecords();
//            if (mRecTagArrayList == null) {
//                mRecTagArrayList = new ArrayList<>();
//            }
//            mRecTagArrayList.clear();
//
//            for (int i = 0; i < mRecTagEntities.size(); i++) {
//                ZHIRRecTagEntity zhirRecTagEntity = mRecTagEntities.get(i);
//                int gtype = ZHIRecTag.TypeNormal;
//                if (zhirRecTagEntity.getContactResult().equalsIgnoreCase(getString(R.string.call_result_ptp))) {
//                    gtype = ZHIRecTag.TypePTP;
//                }
//                ZHIRecTag zhiRecTag = new ZHIRecTag(gtype, zhirRecTagEntity);
//                mRecTagArrayList.add(zhiRecTag);
//            }
//            tv_tr_record_tag_count.setText(mRecTagEntities.size() + "");
//            mZHIRecTagAdjuster.setNewData(mRecTagArrayList);

            //////////////////////////////d01 record list
            ArrayList<ZHIRCaseDetailD01FollowEntity> detailD01FollowEntityArrayList = zhirCaseDetailEntity.getD01Follows();
            tv_dr_d01_record_count.setText(detailD01FollowEntityArrayList.size() + "");
            mZHID01RecordAdjuster.setNewData(detailD01FollowEntityArrayList);

            //////////////////////////////history order list
            ArrayList<ZHIRCaseDetailHistoryOrderEntity> historyOrderEntityArrayList = zhirCaseDetailEntity.getHistoryLoanOrders();
            mZHIHistoryOrderAdjuster.setNewData(historyOrderEntityArrayList);

            if (mPresenter != null) {
                mPresenter.getDunRecords(ZHIDetailActivity.this, new ZHISCaseDetailEntity(userId, orderId, appId));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetDunRecords(ArrayList<ZHIRRecTagEntity> arrayList) {
        try {
            if (mRecTagArrayList == null) {
                mRecTagArrayList = new ArrayList<>();
            }
            mRecTagArrayList.clear();

            if (arrayList == null) {
                arrayList = new ArrayList<>();
            }

            for (int i = 0; i < arrayList.size(); i++) {
                ZHIRRecTagEntity zhirRecTagEntity = arrayList.get(i);
                int gtype = ZHIRecTag.TypeNormal;
                if (zhirRecTagEntity.getContactResult().equalsIgnoreCase(getString(R.string.call_result_ptp))) {
                    gtype = ZHIRecTag.TypePTP;
                }
                ZHIRecTag zhiRecTag = new ZHIRecTag(gtype, zhirRecTagEntity);
                mRecTagArrayList.add(zhiRecTag);
            }

            tv_tr_record_tag_count.setText(arrayList.size() + "");

            mZHIRecTagAdjuster.setNewData(mRecTagArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetDunFollows(ArrayList<ZHIRPhoneBookEntity> arrayList) {
        try {
            mIsFetchPhonebooks = true;

            if (mPhoneBookEntities == null) {
                mPhoneBookEntities = new ArrayList<>();
            }

            if (arrayList == null) {
                arrayList = new ArrayList<>();
            }

            for (int i = 0; i < mPhoneBookEntities.size(); i++) {
                ZHIRPhoneBookEntity zhirPhoneBookEntity = mPhoneBookEntities.get(i);
            }

            mPhoneBookEntities.addAll(arrayList);

            //更新联系人列表
            updatePhoneBookListWithSmsContacts(null);

            if (!mIsFetchSmsBothContacts) {
                if (mPresenter != null) {
                    mPresenter.getSmsBothContacts(ZHIDetailActivity.this, new ZHISCaseDetailEntity(userId, orderId, appId));
                }
                return;
            }
            showPhoneBookView();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetSmsBothContacts(ArrayList<ZHIRPhoneBookSmsEntity> zhirPhoneBookSmsEntityArrayList) {
        try {
            updatePhoneBookListWithSmsContacts(zhirPhoneBookSmsEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            mIsFetchSmsBothContacts = true;
            showPhoneBookView();
        }
    }

    @Override
    public void onCaseRecord() {
        ZHIMainProcessHelper.getInstance().setHadNewCaseRecord(true);
        mZHISubmitRecTagView.dismiss();
        assert mPresenter != null;
        mPresenter.getCaseDetailV2(ZHIDetailActivity.this, new ZHISCaseDetailEntity(userId, orderId, appId));
    }

    @Override
    public void onCaseRecordForWhatsapp(String contactMobile, String content) {
        try {
            Log.d(TAG, "onCaseRecordForWhatsapp: " + contactMobile);
            ZHISysShareHelper.sendMsgWithWhatsApp(ZHIDetailActivity.this, ZHIProjectConstants.PRE_AREA_FLAG + contactMobile, content);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSendTagSms() {
        ToastUtil.s("SMS sent successfully");
    }

    @Override
    public void onSendTagSmsV2() {
        ToastUtil.s("SMS sent successfully");
    }

    @Override
    public void onSendTagSmsLink() {
        ToastUtil.s("SMS sent successfully");
    }

    @Override
    public void onGetRepayLink(ZHIRRepaymentLinkEntity zhirRepaymentLinkEntity) {
        try {
            if (mIsSettled) {
                return;
            }
//            ToastUtil.s("Successful");

            String mRepaymentLink = StringUtil.safeString(zhirRepaymentLinkEntity.getValidUrl());
            //发还款链接
            showSendRepaymentLinkView(tv_fn_full_name.getText().toString().trim(), mMobile, mProductName, mRepaymentLink);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetSmsContext(String smsLink, ZHISSmsContextEntity zhisSmsContextEntity) {
        try {
            if (mIsSettled) {
                return;
            }
            //发还款链接
            showSendSmsLinkView(tv_fn_full_name.getText().toString().trim(), mMobile, mProductName, smsLink);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCreateVirtualAccount() {
        startCutdown();
    }

    @Override
    public void onGetPartRepayList(ArrayList<ZHIRPartRepayListEntity> arrayList) {
        try {
            if (arrayList != null && arrayList.size() > 0) {
                ZHIDialogPartRepay.showDataSource(ZHIDetailActivity.this, arrayList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetUserDemandLetterPdfUrl(ZHIRWarnLetterEntity zhirWarnLetterEntity) {
        if (zhirWarnLetterEntity.isTodaySendFlag()) {
            ZHIDialogMessage.showDialog(getActivity(), "Prompt", zhirWarnLetterEntity.getTips(), true, tag -> {});
            return;
        }
        showSendWarnLetterView(mMobile, zhirWarnLetterEntity.getWhatsappMsg());
    }

    private void updatePhoneBookListWithSmsContacts(ArrayList<ZHIRPhoneBookSmsEntity> zhirPhoneBookSmsEntityArrayList) {
        try {
            if (mPhoneBookEntities == null) {
                mPhoneBookEntities = new ArrayList<>();
            }

            if (zhirPhoneBookSmsEntityArrayList != null && zhirPhoneBookSmsEntityArrayList.size() > 0) {
                int insertIndex = 0;
                if (mPhoneBookEntities.size() >= 3) {
                    insertIndex = 3;
                }

                ArrayList<ZHIRPhoneBookEntity> tempArray = new ArrayList<>();
                for (int i = 0; i < zhirPhoneBookSmsEntityArrayList.size(); i++) {
                    ZHIRPhoneBookSmsEntity zhirPhoneBookSmsEntity = zhirPhoneBookSmsEntityArrayList.get(i);

                    ZHIRPhoneBookEntity zhirPhoneBookEntity = new ZHIRPhoneBookEntity();
                    zhirPhoneBookEntity.setContactMobile(zhirPhoneBookSmsEntity.getContact_mobile());
                    zhirPhoneBookEntity.setContactName(zhirPhoneBookSmsEntity.getContact_name());
                    zhirPhoneBookEntity.setRelation(zhirPhoneBookSmsEntity.getRelation());
                    zhirPhoneBookEntity.setOftenUsed(true);

                    tempArray.add(zhirPhoneBookEntity);
                }

                mPhoneBookEntities.addAll(insertIndex, tempArray);
            }

            for (int i = 1; i < mPhoneBookEntities.size(); i++) {
                if (i >= 3) {
                    break;
                }

                ZHIRPhoneBookEntity zhirPhoneBookEntity = mPhoneBookEntities.get(i);
                zhirPhoneBookEntity.setOftenUsed(true);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.iv_la_amount_detail_ext, R.id.iv_tr_record_tag_ext, R.id.iv_dr_d01_record_ext, R.id.iv_fn_full_name_ext, R.id.iv_bc_bankcard_ext,
            R.id.iv_rs_relation_ship_ext, R.id.iv_cu_contact, R.id.iv_cu_whatsapp,
            R.id.iv_rs_target_relationship0_mark, R.id.iv_rs_target_relationship1_mark,
            R.id.tv_rs_target_relationship_phone0, R.id.tv_rs_target_relationship_phone1,
            R.id.iv_la_app_link_copy, R.id.iv_la_app_name_copy, R.id.iv_bc_bankcard_no_copy,
            R.id.tv_la_repayment_link, R.id.iv_or_offline_repay_ext, R.id.iv_or_recipient_account_name_copy,
            R.id.iv_or_recipient_bank_account_no_copy, R.id.iv_or_recipient_bank_account_name_copy, R.id.tv_or_target_offline_account_get,
            R.id.ll_already_repaid, R.id.iv_ho_history_order_ext, R.id.tv_la_repayment_warn_letter})
    public void onClick(@NonNull View v) {
        switch (v.getId()) {
            case R.id.iv_cu_contact: {
                if (mIsSettled) {
                    return;
                }

                if (!mIsFetchPhonebooks) {
                    if (mPresenter != null) {
                        mPresenter.getDunFollows(ZHIDetailActivity.this, new ZHISCaseDetailEntity(userId, orderId, appId));
                    }
                    return;
                }

                if (!mIsFetchSmsBothContacts) {
                    if (mPresenter != null) {
                        mPresenter.getSmsBothContacts(ZHIDetailActivity.this, new ZHISCaseDetailEntity(userId, orderId, appId));
                    }
                    return;
                }
                showPhoneBookView();
                break;
            }
            case R.id.iv_cu_whatsapp: {
                if (mIsSettled) {
                    return;
                }
//                ZHISysShareHelper.sendMsgWithWhatsApp(ZHIDetailActivity.this,  ZHIProjectConstants.PRE_AREA_FLAG + mMobile, "");

                String relation = "Self";
                ZHISCaseRecordEntity zhiSCaseRecordEntity = getCaseRecordParamsForWhatsapp(tv_cu_name.getText().toString(), mMobile, relation);
                assert mPresenter != null;
                mPresenter.caseRecordForWhatsapp(ZHIDetailActivity.this, zhiSCaseRecordEntity, "");
                break;
            }
            case R.id.tv_la_repayment_link: {
                if (mIsSettled) {
                    return;
                }
                //显示输入框
                showChangeRepayAmountDialog();
                return;
//                assert mPresenter != null;
//                ZHISRepaymentLinkEntity zhiSRepaymentLinkEntity = new ZHISRepaymentLinkEntity(userId, orderId, "CS");
//                mPresenter.getRepayLink(getActivity(), zhiSRepaymentLinkEntity);
//                break;
            }
            case R.id.tv_rs_target_relationship_phone0: {
                if (mIsSettled) {
                    return;
                }

                //统一控制电话操作
                ZHIPhoneCallHelper.getInstance().startPhoneCall(getActivity(), tv_rs_target_relationship_phone0.getTag().toString().trim(), orderId);
                break;
            }
            case R.id.tv_rs_target_relationship_phone1: {
                if (mIsSettled) {
                    return;
                }

                //统一控制电话操作
                ZHIPhoneCallHelper.getInstance().startPhoneCall(getActivity(), tv_rs_target_relationship_phone1.getTag().toString().trim(), orderId);
                break;
            }
            case R.id.iv_rs_target_relationship0_mark: {
                if (mIsSettled) {
                    return;
                }
                showRecTagSubmitView(tv_rs_target_relationship_name0.getText().toString().trim(),
                        tv_rs_target_relationship_phone0.getTag().toString().trim(),
                        tv_rs_target_relationship_relationship0.getText().toString().trim());
                break;
            }
            case R.id.iv_rs_target_relationship1_mark: {
                if (mIsSettled) {
                    return;
                }
                showRecTagSubmitView(tv_rs_target_relationship_name1.getText().toString().trim(),
                        tv_rs_target_relationship_phone1.getTag().toString().trim(),
                        tv_rs_target_relationship_relationship1.getText().toString().trim());
                break;
            }
            case R.id.iv_la_amount_detail_ext: {
                ll_la_amount_content.setVisibility((View.GONE == ll_la_amount_content.getVisibility()) ? View.VISIBLE : View.GONE);
                iv_la_amount_detail_ext.setImageResource((View.GONE == ll_la_amount_content.getVisibility()) ? R.drawable.cf_ic_cf_95 : R.drawable.cf_ic_cf_96);
                break;
            }
            case R.id.iv_tr_record_tag_ext: {
                ll_tr_rec_tag_content.setVisibility((View.GONE == ll_tr_rec_tag_content.getVisibility()) ? View.VISIBLE : View.GONE);
                iv_tr_record_tag_ext.setImageResource((View.GONE == ll_tr_rec_tag_content.getVisibility()) ? R.drawable.cf_ic_cf_95 : R.drawable.cf_ic_cf_96);
                break;
            }
            case R.id.iv_dr_d01_record_ext: {
                ll_dr_d01_record_content.setVisibility((View.GONE == ll_dr_d01_record_content.getVisibility()) ? View.VISIBLE : View.GONE);
                iv_dr_d01_record_ext.setImageResource((View.GONE == ll_dr_d01_record_content.getVisibility()) ? R.drawable.cf_ic_cf_95 : R.drawable.cf_ic_cf_96);
                break;
            }
            case R.id.iv_ho_history_order_ext: {
                ll_ho_history_order_content.setVisibility((View.GONE == ll_ho_history_order_content.getVisibility()) ? View.VISIBLE : View.GONE);
                iv_ho_history_order_ext.setImageResource((View.GONE == ll_ho_history_order_content.getVisibility()) ? R.drawable.cf_ic_cf_95 : R.drawable.cf_ic_cf_96);
                break;
            }
            case R.id.iv_fn_full_name_ext: {
                ll_fn_full_name_content.setVisibility((View.GONE == ll_fn_full_name_content.getVisibility()) ? View.VISIBLE : View.GONE);
                iv_fn_full_name_ext.setImageResource((View.GONE == ll_fn_full_name_content.getVisibility()) ? R.drawable.cf_ic_cf_95 : R.drawable.cf_ic_cf_96);
                break;
            }
            case R.id.iv_bc_bankcard_ext: {
                ll_bc_bankcard_content.setVisibility((View.GONE == ll_bc_bankcard_content.getVisibility()) ? View.VISIBLE : View.GONE);
                iv_bc_bankcard_ext.setImageResource((View.GONE == ll_bc_bankcard_content.getVisibility()) ? R.drawable.cf_ic_cf_95 : R.drawable.cf_ic_cf_96);
                break;
            }
            case R.id.iv_rs_relation_ship_ext: {
                ll_rs_relationship_content.setVisibility((View.GONE == ll_rs_relationship_content.getVisibility()) ? View.VISIBLE : View.GONE);
                iv_rs_relation_ship_ext.setImageResource((View.GONE == ll_rs_relationship_content.getVisibility()) ? R.drawable.cf_ic_cf_95 : R.drawable.cf_ic_cf_96);
                break;
            }
            case R.id.iv_la_app_name_copy: {
                if (mIsSettled) {
                    return;
                }
                ZHISysShareHelper.copyToClipboard(ZHIDetailActivity.this, tv_la_target_app_name.getText().toString().trim());
                break;
            }
            case R.id.iv_la_app_link_copy: {
                if (mIsSettled) {
                    return;
                }
                ZHISysShareHelper.copyToClipboard(ZHIDetailActivity.this, tv_la_target_app_link.getText().toString().trim());
                break;
            }
            case R.id.iv_bc_bankcard_no_copy: {
                if (mIsSettled) {
                    return;
                }
                ZHISysShareHelper.copyToClipboard(ZHIDetailActivity.this, tv_bc_target_bankcard_no.getText().toString().trim());
                break;
            }
            case R.id.iv_or_offline_repay_ext: {
                ll_or_target_offline_repay_content.setVisibility((View.GONE == ll_or_target_offline_repay_content.getVisibility()) ? View.VISIBLE : View.GONE);
                iv_or_offline_repay_ext.setImageResource((View.GONE == ll_or_target_offline_repay_content.getVisibility()) ? R.drawable.cf_ic_cf_95 : R.drawable.cf_ic_cf_96);
                break;
            }
            case R.id.iv_or_recipient_account_name_copy:
            case R.id.iv_or_recipient_bank_account_no_copy:
            case R.id.iv_or_recipient_bank_account_name_copy: {
                if (mIsSettled) {
                    return;
                }
                getOfflineCopyInfo();
                break;
            }
            case R.id.tv_or_target_offline_account_get: {
                if (mIsSettled) {
                    return;
                }
                //倒计时
                if (mPresenter != null) {
                    mPresenter.createVirtualAccount(ZHIDetailActivity.this, new ZHISCreateDunVirtualAccountEntity(userId, orderId));
                }
                break;
            }
            case R.id.ll_already_repaid: {
                if (mPresenter != null) {
                    mPresenter.getPartRepayList(ZHIDetailActivity.this, new ZHISPartRepayListEntity(orderId));
                }
                break;
            }
            case R.id.tv_la_repayment_warn_letter: {
                if (mPresenter != null) {
                    mPresenter.getUserDemandLetterPdfUrl(ZHIDetailActivity.this, new ZHISSendWarnLetterEntity(orderId, "NO"));
                }
                break;
            }
        }
    }

    /**
     * 显示picker view
     *
     * @type method
     * @author liwenjie
     * @creatTime 2020/9/8 09:27
     */
    @SuppressLint("InflateParams")
    private void showRecTagSubmitView(String name, String contact, String relation) {
        try {
            if (mZHISubmitRecTagView == null) {
                mZHISubmitRecTagView = new ZHISubmitRecTagView(getActivity());
                mZHISubmitRecTagView.setFragmentManager(getSupportFragmentManager());
            }
            View parentView = LayoutInflater.from(this).inflate(R.layout.activity_detail_v2, null);
            mZHISubmitRecTagView.setmParentView(parentView);
            mZHISubmitRecTagView.setName(name);
            mZHISubmitRecTagView.setContact(contact);
            mZHISubmitRecTagView.setContactRelation(relation);
            mZHISubmitRecTagView.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示phonebook view
     *
     * @type method
     * @author liwenjie
     * @creatTime 2020/9/8 09:27
     */
    @SuppressLint("InflateParams")
    private void showPhoneBookView() {
        try {
            if (mPhoneBookEntities == null) {
                mPhoneBookEntities = new ArrayList<>();
            }
            if (mPhoneBookEntities.size() == 0) {
                ToastUtil.s("No contacts!");
                return;
            }
            if (mZHIPhoneBookView == null) {
                mZHIPhoneBookView = new ZHIPhoneBookView(getActivity());
            }
            mZHIPhoneBookView.setNewData(mPhoneBookEntities);
            View parentView = LayoutInflater.from(this).inflate(R.layout.activity_detail_v2, null);
            mZHIPhoneBookView.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示send sms view
     *
     * @type method
     * @author liwenjie
     * @creatTime 2020/9/8 09:27
     */
    @SuppressLint("InflateParams")
    private void showSendSmsView(String name, String contact) {
        try {
            if (mZHISendSmsView == null) {
                mZHISendSmsView = new ZHISendSmsView(getActivity());
                mZHISendSmsView.setFragmentManager(getSupportFragmentManager());
            }
            View parentView = LayoutInflater.from(this).inflate(R.layout.activity_detail_v2, null);
            mZHISendSmsView.setmParentView(parentView);
            mZHISendSmsView.setName(name);
            mZHISendSmsView.setContact(contact);
            mZHISendSmsView.setPreContent("");
            mZHISendSmsView.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示send repayment link view
     *
     * @type method
     * @author liwenjie
     * @creatTime 2020/9/8 09:27
     */
    @SuppressLint("InflateParams")
    private void showSendRepaymentLinkView(String name, String contact, String productName, String repaymentLink) {
        try {
            Log.d(TAG, "showSendRepaymentLinkView: " + repaymentLink);
            if (TextUtils.isEmpty(repaymentLink)) {
                return;
            }
            if (mZHISendRepaymentLinkView == null) {
                mZHISendRepaymentLinkView = new ZHISendRepaymentLinkView(getActivity());
                mZHISendRepaymentLinkView.setFragmentManager(getSupportFragmentManager());
            }
            View parentView = LayoutInflater.from(this).inflate(R.layout.activity_detail_v2, null);
            mZHISendRepaymentLinkView.setmParentView(parentView);
            mZHISendRepaymentLinkView.setName(name);
            mZHISendRepaymentLinkView.setContact(contact);
            mZHISendRepaymentLinkView.setRepaymentLink(productName, repaymentLink);
            mZHISendRepaymentLinkView.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示send warn letter view
     *
     * @type method
     * @author liwenjie
     * @creatTime 2020/9/8 09:27
     */
    @SuppressLint("InflateParams")
    private void showSendWarnLetterView(String contact, String content) {
        try {
            if (mZHISendWarnLetterView == null) {
                mZHISendWarnLetterView = new ZHISendWarnLetterView(getActivity());
                mZHISendWarnLetterView.setFragmentManager(getSupportFragmentManager());
            }
            View parentView = LayoutInflater.from(this).inflate(R.layout.activity_detail_v2, null);
            mZHISendWarnLetterView.setmParentView(parentView);
            mZHISendWarnLetterView.setContact(contact);
            mZHISendWarnLetterView.setPreContent(content);
            mZHISendWarnLetterView.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示send sms link view
     *
     * @type method
     * @author liwenjie
     * @creatTime 2020/9/8 09:27
     */
    @SuppressLint("InflateParams")
    private void showSendSmsLinkView(String name, String contact, String productName, String smsLink) {
        try {
            if (TextUtils.isEmpty(smsLink)) {
                return;
            }
            if (mZHISendSmsLinkView == null) {
                mZHISendSmsLinkView = new ZHISendSmsLinkView(getActivity());
                mZHISendSmsLinkView.setFragmentManager(getSupportFragmentManager());
            }
            View parentView = LayoutInflater.from(this).inflate(R.layout.activity_detail_v2, null);
            mZHISendSmsLinkView.setmParentView(parentView);
            mZHISendSmsLinkView.setName(name);
            mZHISendSmsLinkView.setContact(contact);
            mZHISendSmsLinkView.setSmsLink(productName, smsLink);
            mZHISendSmsLinkView.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initTempHUD() {
        mZHIProgressDialogHandler = new ZHIProgressDialogHandler(ZHIDetailActivity.this, "");
    }

    @NonNull
    private ZHISCaseRecordEntity getCaseRecordParams() {
        ZHISCaseRecordEntity zhiSCaseRecordEntity = new ZHISCaseRecordEntity();
        zhiSCaseRecordEntity.setAppId(this.appId);
        zhiSCaseRecordEntity.setUserId(this.userId);
        zhiSCaseRecordEntity.setOrderId(this.orderId);
        zhiSCaseRecordEntity.setSysLoginName(ZHIMainProcessHelper.getInstance().getFullName());
        mZHISubmitRecTagView.fillRecTagParamsV2(zhiSCaseRecordEntity);

        return zhiSCaseRecordEntity;
    }

    @NonNull
    private ZHISCaseRecordEntity getCaseRecordParamsForWhatsapp(String name, String contact, String relation) {
        ZHISCaseRecordEntity zhiSCaseRecordEntity = new ZHISCaseRecordEntity();
        zhiSCaseRecordEntity.setAppId(this.appId);
        zhiSCaseRecordEntity.setUserId(this.userId);
        zhiSCaseRecordEntity.setOrderId(this.orderId);
        zhiSCaseRecordEntity.setSysLoginName(ZHIMainProcessHelper.getInstance().getFullName());
        zhiSCaseRecordEntity.setRemark("whatsapp");

        zhiSCaseRecordEntity.setContactMobile(contact);
        zhiSCaseRecordEntity.setContactName(name);
        zhiSCaseRecordEntity.setContactRelation(relation);
        zhiSCaseRecordEntity.setContactResult("");
        zhiSCaseRecordEntity.setPtpTime("");

        return zhiSCaseRecordEntity;
    }

    @NonNull
    private ZHISSendSmsEntity getSendSmsParamsV2(String content) {
        return new ZHISSendSmsEntity(this.appId, ZHIMainProcessHelper.getInstance().getFullName(), mMobile, content, orderId);
    }

    @NonNull
    private ZHISSendSmsEntity getSendSmsLinkParams(String content) {
        return new ZHISSendSmsEntity(this.appId, mMobile, content, orderId);
    }

    private ZHISRepaymentLinkEntity getRepaymentLinkParams(String amount) {
        ZHISRepaymentLinkEntity zhiSRepaymentLinkEntity = new ZHISRepaymentLinkEntity(userId, orderId);
        if (!TextUtils.isEmpty(amount)) {
            zhiSRepaymentLinkEntity.setChangeRepayType();
            zhiSRepaymentLinkEntity.setRepayAmount(amount);
        }
        return zhiSRepaymentLinkEntity;
    }

    private void showPromtDialog() {
        if (dialogMessage == null) {
            dialogMessage = new ZHIDialogMessage(getActivity());
            dialogMessage.setTitle(getResources().getString(R.string.prompt));
            dialogMessage.setOnClickButtonListener(tag -> {
                dialogMessage.dismiss();
                if (1 == tag) {
                    showSendSmsView(tv_fn_full_name.getText().toString().trim(), mMobile);
                }
            });
        }
        dialogMessage.setMessage("The user does not have an email address and cannot generate a repayment link. You can notify the user by sending a text message");
        dialogMessage.show();
    }

    private void showChangeRepayAmountDialog() {
        if (mZHIDialogChangeAmount == null) {
            mZHIDialogChangeAmount = new ZHIDialogChangeAmount(getActivity());
        }
        mZHIDialogChangeAmount.setOriAmount(mOriAmount);
        mZHIDialogChangeAmount.setDefaultAmount(mRepaymentAmount);
        mZHIDialogChangeAmount.setOnClickButtonListener(changeAmount -> {
            if (!TextUtils.isEmpty(changeAmount)) {
                //获取还款链接
                assert mPresenter != null;
                ZHISRepaymentLinkEntity zhiSRepaymentLinkEntity = getRepaymentLinkParams(changeAmount);
                mPresenter.getRepayLink(getActivity(), zhiSRepaymentLinkEntity);
            }
        });
        mZHIDialogChangeAmount.show();
    }

    @SuppressLint("SetTextI18n")
    private void initCutdown() {
        tv_or_countdown.setText(MAX_CUTDOWN_SEC + "s");
        cp_or_progress.enabledTouch(false);
        cp_or_progress.setDoughnutColors(1);
        cp_or_progress.setOnProgressbarChangeListener(new ZHICircleProgressbar.OnProgressbarChangeListener() {
            @Override
            public void onProgressChanged(ZHICircleProgressbar circleSeekbar, float progressF, boolean fromUser) {
                try {
                    navigationBar.hideLeft();
                    int curProgress = (int) progressF;
                    int resultSec = (int) (curProgress / 100.0F * MAX_CUTDOWN_SEC);

                    if (resultSec >= 1) {
                        tv_or_countdown.setText(resultSec + "s");
                    } else {
                        tv_or_countdown.setText("0s");
                        if (progressF <= 0) {
                            if (mHanler != null) {
                                mHanler.postDelayed(mFinishRunable, 500);
                            } else {
                                mHanler = new Handler(Looper.getMainLooper());
                                mHanler.postDelayed(mFinishRunable, 500);
                            }
                        }
                    }

//                    int progress = (int) (progressF / 10);
//                    if (progressF >= 10) {
//                        tv_or_countdown.setText(progress + "");
//                    } else {
//                        tv_or_countdown.setText("0");
//                        if (progressF <= 0) {
//                            if (mHanler != null) {
//                                mHanler.postDelayed(mFinishRunable, 500);
//                            }
//                        }
//                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStartTracking(ZHICircleProgressbar circleSeekbar) {

            }

            @Override
            public void onStopTracking(ZHICircleProgressbar circleSeekbar) {

            }
        });
    }

    private void startCutdown() {
        try {
            tv_or_target_offline_account_get.setVisibility(View.GONE);
            rl_or_cutdown_bg.setVisibility(View.VISIBLE);
            navigationBar.hideLeft();
            cp_or_progress.setProgressWithAnimation(0, MAX_CUTDOWN_SEC * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("SetTextI18n")
    private void stopCutdown() {
        try {
            navigationBar.showLeft();
            tv_or_countdown.setText(MAX_CUTDOWN_SEC + "s");
            cp_or_progress.setProgress(100);
            tv_or_target_offline_account_get.setVisibility(View.VISIBLE);
            rl_or_cutdown_bg.setVisibility(View.GONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler mHanler = new Handler();

    private Runnable mFinishRunable = () -> {
        try {
            stopCutdown();
            mIsFetchVirtualData = true;
            navigationBar.showRight();
            initData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    };

    private void getOfflineCopyInfo() {
//        ##NAME##
//        This is your repayment account
//        bank name:Wema bank
//        bank card:xxxxxxxxxx

        //Your repayment amount is: xxx, please transfer to this account according to this amount
        String line = tv_cu_name.getText().toString() + ",";
        String line0 = "Your repayment amount is: " + tv_la_repay_amount.getText().toString();
        String line1 = "bank name: " + tv_or_target_recipient_account_name.getText().toString();
        String line2 = "bank card: " + tv_or_target_recipient_bank_account_no.getText().toString();
        String line3 = "app: " + tv_la_target_app_name.getText().toString().trim();
        String content = line + "\n" + line0 + "\n" + line1 + "\n" + line2 + "\n" + line3 + "\n";

        ZHISysShareHelper.copyToClipboard(ZHIDetailActivity.this, content);

//        ZHISysShareHelper.sendMsgWithWhatsApp(ZHIDetailActivity.this,  ZHIProjectConstants.PRE_AREA_FLAG + mMobile, content);

        String relation = "Self";
        ZHISCaseRecordEntity zhiSCaseRecordEntity = getCaseRecordParamsForWhatsapp(tv_cu_name.getText().toString(), mMobile, relation);
        assert mPresenter != null;
        mPresenter.caseRecordForWhatsapp(ZHIDetailActivity.this, zhiSCaseRecordEntity, content);

    }
}